﻿Imports System.Data.SqlClient

Public Class adm_merchantsuccesscaseslist
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Private i As Integer
    Private pages As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim memberId, strErr As String
                Dim companyId As String = ""
                Dim sum, sumdesc As Integer
                i = Request("page")
                If i = Nothing Or i = 0 Then
                    i = 1
                End If
                memberId = Request("uid")
                strErr = ""
                ps.strSql = "select * from merchant where owner_id =@ownerId"
                Dim conn As SqlConnection = objfind.getConn()
                ps.addParameter("ownerId", memberId)
                objrd = objfind.dr(ps, strErr, conn, objrd)
                If (objrd.Read()) Then
                    companyId = objrd.Item("company_id")
                End If
                ps.strSql = "select count(*) from company_successcase where company_id=@companyId and lang_flag='en'"
                ps.parameters.Clear()
                ps.addParameter("companyId", companyId)
                objrd = objfind.dr(ps, strErr, conn, objrd)
                objrd.Read()
                sum = objrd.Item(0)
                pages = (sum \ 10) + 1
                If i >= pages Then
                    i = pages
                ElseIf i <= 0 Then
                    i = 1
                End If
                If (i = pages) Then
                    sumdesc = sum
                Else
                    sumdesc = i * 10
                End If
                totalPage.InnerText = i.ToString & "/" & pages.ToString()
                ps.strSql = "select top(" & i & "*10) * from company_successcase where company_id=@companyId and lang_flag='en'" & _
                        "and id not in (" & _
                        "select top (" & i - 1 & "*10) id from company_successcase where company_id=@companyId and lang_flag='en' " & _
                        "order by title" & _
                        ") and id not in (select top(" & sum - sumdesc & ") id from company_successcase where company_id=@companyId and lang_flag='en'" & _
                        "order by title desc) order by title"
                'ps.strSql = "select * from company_successcase where company_id=@companyId and lang_flag='en'"
                ps.parameters.Clear()
                ps.addParameter("companyId", companyId)
                objrd = objfind.dr(ps, strErr, conn, objrd)
                purchaseList.InnerHtml = "<table width=""98%"" border=""0"" cellpadding=""0"" cellspacing=""1"" style=""margin-top:10px;"">" & _
                                             "<tr>" & _
                                                 "<td width=""50%"" bgcolor=""#CCCCCC"">Title</td>" & _
                                                 "<td width=""20%"" bgcolor=""#CCCCCC"">Create Time</td>" & _
                                                 "<td width=""30"" bgcolor=""#CCCCCC"">Option</td>" & _
                                             "</tr>"
                While (objrd.Read())
                    purchaseList.InnerHtml += "<tr>" & _
                                                "<td bgcolor=""#F5F5F5""> " & objrd("title") & "</td>" & _
                                                "<td bgcolor=""#F5F5F5""> " & objrd("create_date") & "</td>" & _
                                                "<td bgcolor=""#F5F5F5""> <a href=""adm_merchantsuccesscases.aspx?editType=delete&successId=" & objrd("id") & "&uid=" & Request("uid") & """ >delete</a>&nbsp&nbsp&nbsp&nbsp<a href=""adm_merchantsuccesscases.aspx?successId=" & objrd("id") & "&uid=" & Request("uid") & """>edit</a>" & _
                                            "</tr>"
                End While
                objfind.closeConn(conn)
                purchaseList.InnerHtml += "</table>"
                pageNav.InnerHtml = "<a href=""adm_merchantsuccesscaseslist.aspx?page=1" & "&uid=" & Request("uid") & """>home &nbsp;</a>" & _
                                    "<a href=""adm_merchantsuccesscaseslist.aspx?page=" & i - 1 & "&uid=" & Request("uid") & """>previous page &nbsp;</a>" & _
                                    "<a href=""adm_merchantsuccesscaseslist.aspx?page=" & i + 1 & "&uid=" & Request("uid") & """>next page &nbsp;</a>" & _
                                    "<a href=""adm_merchantsuccesscaseslist.aspx?page=" & pages & "&uid=" & Request("uid") & """>end &nbsp;</a>"
            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submit.Click
        Response.Redirect("adm_merchantsuccesscaseslist.aspx?page=" & pageNum.Text & "&uid=" & Request("uid") & "")
    End Sub

End Class